<?php
// +----------------------------------------------------------------------
// | Tpp
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.intaotao.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 黄海 < 15679824@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use \think\Cache;
use \think\Controller;
use think\Loader;
use think\Db;
use app\common\utils\ResultUtil;
use \think\facade\Cookie;
use \think\facade\Session;
use app\admin\controller\Permissions;
use app\common\utils\UniqueUtil;




class Config extends Permissions
{
    public function configList()
    {
        $config_list = Db::name('sys_config')->select();
        $this->assign('config_list',$config_list);
        return $this->fetch('config/config_list');
    }
    public function configInfo($config_id='')
    {
        if($config_id){
            $config_info = Db::name('sys_config')->where('config_id',$config_id)->find();
            if($config_info['config_val_mode'] == 1){
                $config_val_arr = json_decode($config_info['config_val'],true);
                //pre_print($config_val_arr);
                if($config_val_arr){
                    foreach($config_val_arr as $key => $val){
                        $config_val_arr_list [$key]['key1'] = $key;
                        $config_val_arr_list [$key]['val1'] = $val;
                    }

                    $config_info['config_val_arr_list'] = $config_val_arr_list;
                }else{
                    $config_info['config_val_arr_list'] = $config_info['config_val'];
                }
            }else{
                $config_info['config_val_arr_list'] = null;
            }
        }else{
            $config_info = [
                'config_val_mode'=>0,
                'config_val_arr_list' =>null,
                'config_key'=>''
            ];
        }
        
        $this->assign('configInfo',$config_info);
        return $this->fetch('config/config_info');
    }

    public function configEdit()
    {
    	if($this->request->isPost()) {
            //是提交操作
            $post = $this->request->post();
            //$post['update_time']=time();
            //验证  唯一规则： 表名，字段名，排除主键值，主键名
            $validate = new \think\Validate;
            $validate->rule([
                    'config_type|参数类别'  => 'require',
                    'config_key|参数名称'  => 'require',
                    'config_val|参数值'  => 'require',

            ]);
            
            if (isset($post['config_val_1'])) {//如果是多个选项
                foreach ($post['config_val_1'] as $key => $val) {
                    $resource_attr_data[$val] = $post['config_val_2'][$key];
                }
                
                $post['config_val'] = json_encode($resource_attr_data,320);
                unset($post['config_val_1']);
                unset($post['config_val_2']);
            }
            
            //验证部分数据合法性
            if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
            }

            
            
            try {
                    Db::name('sys_config')->update($post);
                    $flag = true;
            } catch (\Exception $e) {
                    $flag = false;
                    $error = $e->getMessage();//抛出异常
            }
            if($flag){
                    return $this->success('修改成功','admin/config/configList');
            } else {
               return $this->error($error.'修改失败');
            } 
					
    	} else {
    		return $this->error('非法请求');
    	}
    }
    public function configAdd()
    {
        if($this->request->isPost()) {
            //是提交操作
            $post = $this->request->post();
            //$post['update_time']=time();
            //验证  唯一规则： 表名，字段名，排除主键值，主键名
            $validate = new \think\Validate;
            $validate->rule([
                    'config_type|参数类别'  => 'require',
                    'config_key|参数名称'  => 'require',
                    'config_val|参数值'  => 'require',

            ]);
            if (isset($post['config_val_1'])) {//如果是多个选项
                foreach ($post['config_val_1'] as $key => $val) {
                    $resource_attr_data[$val] = $post['config_val_2'][$key];
                }
                
                $post['config_val'] = json_encode($resource_attr_data,320);
                unset($post['config_val_1']);
                unset($post['config_val_2']);
            }

            //验证部分数据合法性
            if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
            }
            if(false == Db::name('sys_config')->insert($post)) {
                return $this->error('添加失败');
            } else {
                return $this->success('添加成功','admin/config/configList');
            }
        } else {
            return $this->error('非法请求');
        }
    }
    public function configDelete()
    {
        
            $config_id = input('config_id');
            $flag=Db::name('sys_config')->delete($config_id);
            if($flag) {
                 return $this->success('删除成功','admin/config/configList');
            } else {
                    return $this->error('删除失败');
            }
        
    }
}
